package mudclient;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.GroupLayout;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;
import javax.swing.ScrollPaneConstants;

public class MudClientMainFrame extends JFrame {
	public MudClientMainFrame() {
		super("MudClient");
		
		initComponents();
	}
	
	private void initComponents() {
		
		inputTextPaneScrollPane = new JScrollPane();
		outputTextAreaScrollPane = new JScrollPane();
		inputTextPane = new MudClientTextPane(inputTextPaneScrollPane.getVerticalScrollBar());
		outputTextArea = new JTextArea();
		jmb = new JMenuBar();
		fileMenu = new JMenu();
		connectMenuItem = new JMenuItem();
		
		inputTextPane.setBackground(Color.black);
		inputTextPane.setForeground(Color.white);
		inputTextPane.setDisabledTextColor(Color.white);
		inputTextPaneScrollPane.setViewportView(inputTextPane);
		inputTextPaneScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		inputTextPane.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				e.consume();
				outputTextArea.grabFocus();
			}
		});
		
		outputTextArea.setBackground(Color.black);
		outputTextArea.setForeground(Color.white);
		outputTextAreaScrollPane.setViewportView(outputTextArea);
		outputTextAreaScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		outputTextAreaScrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		
		fileMenu.setText("File");
		fileMenu.setMnemonic(KeyEvent.VK_F);
		jmb.add(fileMenu);
		
		connectMenuItem.setText("Connect");
		connectMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, ActionEvent.CTRL_MASK));
		final JFrame myself = this;
		connectMenuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				if(mcd == null) {
					mcd = new MudClientConnectDialog(myself);
					mcd.addOKActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							mcd.setVisible(false);
							new TelnetThread(mcd.getHostName(), Integer.parseInt(mcd.getPort()), inputTextPane, outputTextArea).start();
						}
					});
				}
				mcd.setVisible(true);
			}
		});
		fileMenu.add(connectMenuItem);
		
		this.setJMenuBar(jmb);
		
		GroupLayout gp = new GroupLayout(getContentPane());
		getContentPane().setLayout(gp);
		gp.setHorizontalGroup(
			gp.createParallelGroup().
				addComponent(inputTextPaneScrollPane, GroupLayout.DEFAULT_SIZE, 697, Short.MAX_VALUE).
				addComponent(outputTextAreaScrollPane, GroupLayout.DEFAULT_SIZE, 697, Short.MAX_VALUE)
		);
		gp.setVerticalGroup(
			gp.createSequentialGroup().
				addComponent(inputTextPaneScrollPane, GroupLayout.DEFAULT_SIZE, 819, Short.MAX_VALUE).
				addComponent(outputTextAreaScrollPane, GroupLayout.DEFAULT_SIZE, 37, GroupLayout.PREFERRED_SIZE)
		);
		
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		this.pack();
	}
	
	@Override
	public void dispose() {
		if(inputTextPane != null) {
			TelnetThread rt = inputTextPane.getReaderThread();
			if(rt != null)
				rt.setRunning(false);
			super.dispose();
			System.exit(0);
		}
	}
	
	public void setVisible(boolean b) {
		super.setVisible(b);
		outputTextArea.grabFocus();
	}
	
	private MudClientTextPane inputTextPane;
	private JTextArea outputTextArea;
	private JScrollPane inputTextPaneScrollPane;
	private JScrollPane outputTextAreaScrollPane;
	private JMenuBar jmb;
	private JMenu fileMenu;
	private JMenuItem connectMenuItem;
	
	private MudClientConnectDialog mcd;
}
